<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/4/17 0017
 * Time: 11:49
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');

class Jiankang_ask extends CI_Controller
{
    public function info(){
        $redis = new Redis();
        $redis->connect('127.0.0.1');
        $redis->auth('jhcms');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $urlid = $redis->lpop('jiankang_ask_list');
        //$urlid = 1272772;
        if(!$urlid){
            echo "nodata";
            exit;
        }

        $url_info = $this->db->query("select * from res_ask_res where arc_id=".$urlid)->row_array();
        $ask_id = $url_info['arc_id'];

        //echo "采集开始:".time()."-";
        $detail_url = "https://www.99.com.cn/wenda/".$ask_id.".html";
        $content = geturl($detail_url);
        $html = str_get_html($content);
        // 处理对应字段
        $department_1 = $url_info['dept'];
        $ask_title = $url_info['ask_title'];
        $ask_date = $url_info['ask_date'];
        $click_num = $url_info['click_num'];
        $ask_detail = $html->find('div[class=atcle-ms]',0)->innertext;
        $ask_detail = trim(str_replace('<span>病情描述：</span>','',$ask_detail));
        $sql = "";
        foreach ($html->find('div[class=dtl-list]',0)->find("dl[class=dtl-ys]") as $k=>$ys){
            $author_name = trim($ys->find('dd',0)->find('b',0)->find('a',0)->innertext);
            $author_hospital = trim($ys->find('dd',0)->find('b',0)->find('a',1)->innertext);
            $author_departemt = trim($ys->find('dd',0)->find('b',0)->find('span',0)->innertext);
            $author_title = trim($ys->find('dd',0)->find('b',0)->find('em',0)->innertext);
            $answer_content = $html->find("div[class=dtl-reply]",$k);

            if($answer_content->find('i',0)){
                $is_accept = 1;
            }else{
                $is_accept = 0;
            }

            $answer_detail = $answer_content->innertext;
            $answer_detail = str_replace('病情分析:','',$answer_detail);

            $answer_detail = str_replace('<em></em>','',$answer_detail);
            $answer_detail = trim(str_replace('<i></i>','',$answer_detail));
            $answer_date = $html->find("div[class=dtl-time]",$k)->find('span',0)->innertext;
            $sql .= "('".$department_1."','".$ask_title."','".$ask_detail."','".$ask_date."','".$answer_date."','".$answer_detail."','".$is_accept."','".$click_num."','".$author_name."','".$author_title."','".$author_departemt."','".$author_hospital."','".$ask_id."'),";
        }
        $sql = substr($sql, 0, -1);
        $sql = "INSERT ignore INTO ai_spider_39_disease_ask(department_1,ask_title,ask_detail,ask_date,answer_date,answer_detail,accept,click_num,author_name,author_title,author_department,author_hospital,vid) VALUES ".$sql;
        $this->db->query($sql);
        echo 1;
        //$ = $html->find("ul[id=videoList] li");
    }


    public function urls($dep=1){
        $total = file_get_contents("https://www.99.com.cn/wenda/getSearchCount.json?id=".$dep."&type=dep");
        $limit = 10000;
        $pages = ceil($total/$limit);
        $depts = [
            1=>'内科',
            2=>'外科',
            3=>'妇产科',
            4=>'儿科',
            5=>'男科',
            6=>'皮肤性病科',
            7=>'五官科',
            8=>'肿瘤科',
            9=>'精神心理科',
            10=>'不孕不育',
            11=>'其他',
        ];
        for($p=1;$p<=$pages;$p++){
            //echo "https://www.99.com.cn/wenda/searchlist.json?page=".$p."&limit=".$limit."&id=".$dep."&type=dep";exit;
            $content = file_get_contents("https://www.99.com.cn/wenda/searchlist.json?page=".$p."&limit=".$limit."&id=".$dep."&type=dep");
            $contents = json_decode($content,true);
            $sql = "";
            if($contents['data']){
                foreach($contents['data'] as $con){
                    $arc_id = $con['ID'];
                    $ask_title = $con['AskTitle'];
                    $ask_date = $con['CreateDate'];
                    $click_num = $con['AskClick'];
                    $dept = $depts[$dep];
                    $sql .= "('".$arc_id."','".$dept."','".$ask_title."','".$ask_date."','".$click_num."'),";
                }
                $sql = substr($sql, 0, -1);
                $sql = "INSERT ignore INTO res_ask_res(arc_id,dept,ask_title,ask_date,click_num) VALUES ".$sql;
                //echo $sql;exit;
                $this->db->query($sql);
                echo "ok";
            }

        }

    }

    public function in_list(){
        $redis = new Redis();
        $redis->connect('127.0.0.1');
        $redis->auth('jhcms');
        // 查询所有url_id 不匹配的数据
        $normal = $this->db->query("select vid from ai_spider_39_disease_ask group by vid")->result_array();
        $nor_ids = array_column($normal,'vid');
        $tmp = $this->db->query("select arc_id from res_ask_res")->result_array();
        $tmp_ids = array_column($tmp,'arc_id');
        $diff = array_diff($tmp_ids,$nor_ids);
        //$vids = implode(",",$diff);
        //$list = $this->db->query("select id from ai_39_disease_video where vid in(".$vids.")")->result_array();
        foreach($diff as $vo){
            $redis->rPush('jiankang_ask_list',$vo);
        }

    }

    public function cid_push(){

        $redis = new Redis();
        $redis->connect('127.0.0.1');
        $redis->auth('jhcms');
        $ids = $this->db->query("select id from res_ask_res where id>=17403")->result_array();
        foreach($ids as $id){
            $redis->rPush('jiankang_ask_list',$id['id']);
        }

    }
}